using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using GGEditor.API;
using GGEditor.API.Plugins;

namespace GGEditor.Core.UI
{
	[XmlNodeObjectFactory(Name = "WorkspaceWindow")]
	public class WorkspaceWindowFactory : IXmlNodeObjectFactory
	{
		#region Implementation of IXmlNodeObjectFactory

		public object Build(IPluginNode pluginNode, IDictionary<string, string> attributes)
		{
			string formTypeName = attributes["type"];
			IWorkspaceForm form = null;

			foreach (Assembly assembly in pluginNode.Plugin.Assemblies)
			{
				try
				{
					form = assembly.CreateInstance(formTypeName) as IWorkspaceForm;
					break;
				}
				catch (Exception) { }
			}

			if (form == null)
				throw new ArgumentException();

			return form;
		}

		#endregion
	}
}